package dto

import (
	"danmu/common"
	"danmu/model"
	"time"
)


type MessageListDto struct {
	Fid     uint   `json:"fid"`
	Name string `json:"name"`
	Face string `json:"face"`
	Content string `json:"content"`
	Date    time.Time `json:"date"`
}

type MessageByIdDto struct {
	Fid     uint   `json:"fid"`
	From    uint   `json:"from"`
	Content string `json:"content"`
	Date    time.Time `json:"date"`
}

func ToMessageListDto(messages []model.Message) []MessageListDto {
	DB :=common.GetDB()
	newMessage := make([]MessageListDto, len(messages))
	for i := 0; i < len(messages); i++ {
		newMessage[i].Fid = messages[i].Fid
		newMessage[i].Content = messages[i].Content
		newMessage[i].Date = messages[i].CreatedAt
		//查询用户信息
		var userInfo model.User
		DB.First(&userInfo, messages[i].Fid)
		newMessage[i].Name = userInfo.Name
		newMessage[i].Face = userInfo.Face
	}
	return newMessage
}


func ToMessageByIdDto(messages []model.Message) []MessageByIdDto {

	newMessage := make([]MessageByIdDto, len(messages))
	for i := 0; i < len(messages); i++ {
		newMessage[i].Fid = messages[i].Fid
		newMessage[i].From = messages[i].FromId
		newMessage[i].Content = messages[i].Content
		newMessage[i].Date = messages[i].CreatedAt
	}
	return newMessage
}
